package com.example.myapplication.apter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.OrderlistBean;

import org.json.JSONObject;

import java.util.List;

import takeout.TakeoutActivity;
import takeout.home.OrderActivity;
import takeout.home.ShopdetailsActivity;
import takeout.order.OrderdetailsActivity;
import test.IntenfaceTest;

public class OrderlistApter extends RecyclerView.Adapter<OrderlistApter.MyHolder> {
    private Context context;
    private View view;
    private List<OrderlistBean.RowsDTO> list;
    private String TAG = "TAG";
   // private OnOrderlistClickListener listener ;
    private OnOrderlistClickListener listener ;


    public OrderlistApter(Context context, List<OrderlistBean.RowsDTO> list, OnOrderlistClickListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    /*public OrderlistApter(Context context, List<OrderlistBean.RowsDTO> list, IntenfaceTest listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }*/

    //适配器中定义一个接口
    public interface OnOrderlistClickListener {
        void onOrderlistClick(String list);
        void onprefundlistClick(String order);
    }

    @NonNull
    @Override
    public OrderlistApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_orderlist, null);
        return new OrderlistApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderlistApter.MyHolder holder, int position) {
        OrderlistBean.RowsDTO.SellerInfoDTO sellerInfo = list.get(position).getSellerInfo();
        OrderlistBean.RowsDTO.OrderInfoDTO orderInfo = list.get(position).getOrderInfo();


        if (sellerInfo != null) {
            if (!TextUtils.isEmpty(sellerInfo.getImgUrl())) {
                Glide.with(context).load(TokenBean.IP + sellerInfo.getImgUrl()).into(holder.imgUrl);
            }
            if (!TextUtils.isEmpty(sellerInfo.getName())) {
                holder.name.setText(sellerInfo.getName());
            }
        }

        if (orderInfo != null) {
            List<OrderlistBean.RowsDTO.OrderInfoDTO.OrderItemListDTO> orderItemList = orderInfo.getOrderItemList();
            if (orderItemList != null) {
                holder.nums.setText(String.valueOf(orderItemList.size()));
                @SuppressLint("WrongConstant") LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayout.HORIZONTAL, false);
                holder.foodlist.setLayoutManager(linearLayoutManager);
                holder.foodlist.setAdapter(new FoodApter(context, orderItemList));
            }

            if (!TextUtils.isEmpty(orderInfo.getStatus())) {
                holder.status.setText(orderInfo.getStatus());
                //支付订单号
                String orderNo = orderInfo.getOrderNo();
                //支付的金额
                Double amount = orderInfo.getAmount();
                if ("待支付".equals(orderInfo.getStatus())) {
                    holder.payment.setVisibility(View.VISIBLE);
                    holder.re2.setVisibility(View.GONE);
                    holder.singleorder.setVisibility(View.GONE);
                    holder.payment.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //跳转到支付页面
                            JumpOrderActivity(orderNo, amount);
                        }
                    });
                } else if ("待评价".equals(orderInfo.getStatus())) {
                    holder.re2.setVisibility(View.VISIBLE);
                    holder.payment.setVisibility(View.GONE);
                    holder.singleorder.setVisibility(View.GONE);
                    //评论
                    holder.comment.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Jumpcomment(orderNo);
                        }
                    });
                    //退款
                    holder.refund.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Jumprefund(orderNo);
                        }
                    });
                } else if ("完成".equals(orderInfo.getStatus()) && sellerInfo != null) {
                    holder.re2.setVisibility(View.GONE);
                    holder.payment.setVisibility(View.GONE);
                    holder.singleorder.setVisibility(View.VISIBLE);
                    holder.singleorder.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //由于api不全,这里先使用有数据的
                            // JumpOrderShopdetailsActivity(finalSellerInfo.getId());
                            JumpOrderShopdetailsActivity(5);
                        }
                    });
                } else if ("退款".equals(orderInfo.getStatus())) {
                    holder.re2.setVisibility(View.GONE);
                    holder.payment.setVisibility(View.GONE);
                    holder.singleorder.setVisibility(View.GONE);
                }
            }
            if (!TextUtils.isEmpty(String.valueOf(orderInfo.getAmount()))) {
                holder.monrytotle.setText(String.valueOf(orderInfo.getAmount()));
            }
        }


        //点击进入订单详情
        if (orderInfo != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, OrderdetailsActivity.class);
                    intent.putExtra(Commom.ORDERNO, String.valueOf(orderInfo.getOrderNo()));
                    context.startActivity(intent);
                }
            });
        }
    }

    private void JumpOrderShopdetailsActivity(Integer id) {
        Intent intent = new Intent(context, ShopdetailsActivity.class);
        intent.putExtra(Commom.STOREINFORMATIONID, String.valueOf(id));
        context.startActivity(intent);
    }

    private void Jumprefund(String orderNo) {
        listener.onprefundlistClick(orderNo);
    }

    private void Jumpcomment(String orderNo) {
     //   String[] str = {orderNo,"xxxx","x111"};
       // listener.returnint(str);
        listener.onOrderlistClick(orderNo);
    }



    private void JumpOrderActivity(String orderNo, Double amount) {
        Intent intent = new Intent(context, OrderActivity.class);
        intent.putExtra(Commom.ADDRESS, orderNo);
        intent.putExtra(Commom.MONEYTOTAL, String.valueOf(amount));
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }




    class MyHolder extends RecyclerView.ViewHolder {
        ImageView imgUrl;
        TextView name;
        TextView status;
        RecyclerView foodlist;
        TextView nums;
        TextView monrytotle;
        TextView payment;
        TextView comment;
        TextView refund;
        TextView singleorder;
        RelativeLayout rootView;
        RelativeLayout re2;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgUrl = itemView.findViewById(R.id.imgUrl);
            name = itemView.findViewById(R.id.name);
            status = itemView.findViewById(R.id.status);
            foodlist = itemView.findViewById(R.id.foodlist);
            nums = itemView.findViewById(R.id.nums);
            monrytotle = itemView.findViewById(R.id.monrytotle);
            payment = itemView.findViewById(R.id.payment);
            rootView = itemView.findViewById(R.id.rootView);
            re2 = itemView.findViewById(R.id.re2);
            refund = itemView.findViewById(R.id.refund);
            comment = itemView.findViewById(R.id.comment);
            singleorder = itemView.findViewById(R.id.singleorder);
        }
    }
}

